package com.keepers.repair.service;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;

import com.blankj.utilcode.util.Utils;
import com.keepers.repair.activity.SinglePixelActivity;
import com.keepers.repair.net.LogUtil;

import java.lang.ref.WeakReference;

/**
 * 1像素管理类
 * <p>
 * Created by jianddongguo on 2017/7/8.
 * http://blog.csdn.net/andrexpert
 */

public class ScreenManager {
    @SuppressLint("StaticFieldLeak")
    private static ScreenManager           screenManager;
    // 使用弱引用，防止内存泄漏
    private        WeakReference<Activity> mActivityRef;

    private ScreenManager() {
    }

    // 单例模式
    public static ScreenManager getScreenManagerInstance() {
        if (screenManager == null) {
            screenManager = new ScreenManager();
        }
        return screenManager;
    }

    // 获得SinglePixelActivity的引用
    public void setSingleActivity(Activity mActivity) {
        mActivityRef = new WeakReference<>(mActivity);
    }

    // 启动SinglePixelActivity
    public void startActivity() {
        LogUtil.e("准备启动SinglePixelActivity...");
        Intent intent = new Intent(Utils.getApp(), SinglePixelActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Utils.getApp().startActivity(intent);
    }

    // 结束SinglePixelActivity
    public void finishActivity() {
        LogUtil.e("准备结束SinglePixelActivity...");
        if (mActivityRef != null) {
            Activity mActivity = mActivityRef.get();
            if (mActivity != null) {
                mActivity.finish();
            }
        }
    }
}
